package cn.project.engineering.receiver

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import cn.project.engineering.constant.GwConstant
import cn.project.engineering.constant.LiveEventBusConstant
import cn.project.engineering.utils.AlarmManagerUtil
import com.jeremyliao.liveeventbus.LiveEventBus
import log.KLog

/**
 *
 * @author  lifuhai
 * @date 2021/2/20 14:10
 */
class ScreenManagerReceiver : BroadcastReceiver() {
    companion object {
        private const val TAG = "ScreenManagerReceiver"
    }

    override fun onReceive(context: Context?, intent: Intent?) {
        if (AlarmManagerUtil.SCREEN_CONTROL_ACTION == intent?.action) {
            val intervalMillis = intent.getLongExtra("intervalMillis", 0)

            if (0L != intervalMillis) {
                AlarmManagerUtil.setAlarmTime(context!!, System.currentTimeMillis() + intervalMillis, intent)
            }

            when (intent.getIntExtra("id", 0)) {
                GwConstant.OPEN_SCREEN_ALARM_ID -> {
                    // 开启屏幕
                    KLog.d(TAG, "打开屏幕")
                    LiveEventBus.get(LiveEventBusConstant.OPEN_SCREEN_EVENT).post("")
                }
                GwConstant.CLOSE_SCREEN_ALARM_ID -> {
                    // 关闭屏幕
                    KLog.d(TAG, "关闭屏幕")
                    LiveEventBus.get(LiveEventBusConstant.CLOSE_SCREEN_EVENT).post("")
                }
            }
        }
    }
}